/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-29 10:03:02
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-02 11:21:24
 */
import bagCtr from "../ctrs/bags/bagCtr";
import { bagEntity } from "../ctrs/bags/bagEntity";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import { GameFacade } from "../libs/core/GameFacade";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { INotification } from "../libs/pureMvc/Interfaces";
const { ccclass, property } = cc._decorator;
@ccclass
export default class bagItemCom extends BaseGameComponent {

  @property({ type: cc.Sprite, tooltip: "图片icon" })
  icon: cc.Sprite = null;

  @property({ type: cc.Label, tooltip: "使用次数" })
  timeTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "名称" })
  nameTxt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "是否选中" })
  isSelect: cc.Node = null;

  private _bagEny: bagEntity = null;
  drawUi(data) {
    if (!this._bagEny) {
      this._bagEny = data;
    }
    this.nameTxt.string = data.name;
    this.timeTxt.string = `次数:${data.time}/${data.maxTime}`;
    cc.resources.load("bagIcon/" + this._bagEny.id, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
      if (err) return;
      this.icon.spriteFrame = sp;
    })
  }


  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_BAGCOM_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    let body = notification.getBody();
    switch (evtName) {
      case PureMVCEvents.ON_BAGCOM_UDPATE:
        if (bagCtr.ins.curEny && this._bagEny.id == bagCtr.ins.curEny.id) {
          this.isSelect.active = true;
        } else {
          this.isSelect.active = false;
        }
        break;
    }
  }

  onSelect() {
    bagCtr.ins.curEny = this._bagEny;
    GameFacade.getInstance().sendNotification(PureMVCEvents.ON_BAGCOM_UDPATE);
  }
}